﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using AppManagement.Repositories;
using AppManagement.Helpers;
using AppManagement.Models;
using AppManagement.Reports;

namespace AppManagement.MainManagers
{
    public partial class FrmBangDiemHocPhan : DevComponents.DotNetBar.Office2007Form
    {
        public EventHandler Handler { get; set; }
        private DevComponents.DotNetBar.Controls.TextBoxX txtMaKhoa;
        private DataTable DataSource;

        public FrmBangDiemHocPhan()
        {
            InitializeComponent();
        }

        public void InitHiddenFields()
        {
            txtMaKhoa = new DevComponents.DotNetBar.Controls.TextBoxX
            {
                Name = "txtMaKhoa",
                Text = string.Empty
            };
            txtMaKhoa.Visible = false;
        }

        private void btnChonKhoa_Click(object sender, EventArgs e)
        {
            FrmQuanLyKhoa frm = new FrmQuanLyKhoa(true);
            frm.Handler += GetKhoa;
            frm.ShowDialog();
        }

        public void GetKhoa(object sender, EventArgs e)
        {
            var eventType = (TransferSimpleDataEvent)e;
            string[] comp = eventType.Data.Split(new char[] { '#' });
            txtMaKhoa.Text = comp[0];
            txtKhoa.Text = comp[1];

            var lstChuyenNganh = ChuyenNganhRepository.SelectByMaKhoa(int.Parse(txtMaKhoa.Text));
            cbxChuyenNganh.Items.Clear();
            foreach (var item in lstChuyenNganh)
            {
                cbxChuyenNganh.Items.Add(item);
            }
        }

        private void cbxChuyenNganh_SelectedIndexChanged(object sender, EventArgs e)
        {
            var chuyenNganh = (ChuyenNganh)cbxChuyenNganh.SelectedItem;
            var lstKHDT = KeHoachDaoTaoRepository.SelectByMaCN(chuyenNganh.MaCN);
            cbxKHDT.Items.Clear();
            foreach (var item in lstKHDT)
            {
                cbxKHDT.Items.Add(item);
            }
        }

        private void cbxKHDT_SelectedIndexChanged(object sender, EventArgs e)
        {
            var khdt = KeHoachDaoTaoRepository.SelectByID(((KeHoachDaoTao)cbxKHDT.SelectedItem).MaKHDT);

            if (khdt != null)
            {
                var lstCTKHDT = ChiTietKHDTRepository.SelectByMaKHDT(khdt.MaKHDT);
                cbxHocPhan.Items.Clear();
                foreach (var item in lstCTKHDT)
                {
                    cbxHocPhan.Items.Add(item);
                }
            }
        }

        private void InitGridView()
        {
            DataGridViewTextBoxColumn objColumn = new DataGridViewTextBoxColumn
            {
                HeaderText = "MSSV",
                DataPropertyName = "MSSV",
                Name = "MSSV",
                Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth) * ((double)1 / 6)) - 30
            };
            dtgvData.Columns.Add(objColumn);

            objColumn = new DataGridViewTextBoxColumn
            {
                HeaderText = "HoTen",
                DataPropertyName = "HoTen",
                Name = "HoTen",
                Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth) * ((double)1 / 6)) + 30
            };
            dtgvData.Columns.Add(objColumn);

            objColumn = new DataGridViewTextBoxColumn
            {
                HeaderText = "DiemQuaTrinh",
                DataPropertyName = "DiemQuaTrinh",
                Name = "DiemQuaTrinh",
                Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth) * ((double)1 / 6))
            };
            dtgvData.Columns.Add(objColumn);

            objColumn = new DataGridViewTextBoxColumn
            {
                HeaderText = "DiemThi",
                DataPropertyName = "DiemThi",
                Name = "DiemThi",
                Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth) * ((double)1 / 6))
            };
            dtgvData.Columns.Add(objColumn);

            objColumn = new DataGridViewTextBoxColumn
            {
                HeaderText = "DiemTongKet",
                DataPropertyName = "DiemTongKet",
                Name = "DiemTongKet",
                Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth) * ((double)1 / 6))
            };
            dtgvData.Columns.Add(objColumn);

            objColumn = new DataGridViewTextBoxColumn
            {
                HeaderText = "DiemThiLan2",
                DataPropertyName = "DiemThiLan2",
                Name = "DiemThiLan2",
                Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth) * ((double)1 / 6)) - 1
            };
            dtgvData.Columns.Add(objColumn);
        }

        public void InitDataSource()
        {
            DataSource = new DataTable();
            DataSource.Columns.Add("MSSV");
            DataSource.Columns.Add("HoTen");
            DataSource.Columns.Add("DiemQuaTrinh");
            DataSource.Columns.Add("DiemThi");
            DataSource.Columns.Add("DiemTongKet");
            DataSource.Columns.Add("DiemThiLan2");
            DataView objTimeSheetDV = new System.Data.DataView(DataSource);
            objTimeSheetDV.AllowNew = false;
            dtgvData.AutoGenerateColumns = false;
            dtgvData.DataSource = objTimeSheetDV;
            dtgvData.ClearSelection();
        }

        private void cbxHocPhan_SelectedIndexChanged(object sender, EventArgs e)
        {
            var lstSVDangKy = ChiTietDangKyRepository.SelectByMaCTKHDT(((ChiTietKHDT)cbxHocPhan.SelectedItem).MaCTKHDT);
            DataSource.Rows.Clear();
            foreach (var item in lstSVDangKy)
            {
                var lstDiemht = DiemHocTapRepository.SelectByMaCTDK(item.MaCTDK);
                if (lstDiemht.Count > 0)
                {
                    DataSource.Rows.Add(item.PhieuDangKy.SinhVien.MSSV, item.PhieuDangKy.SinhVien.HoTen, lstDiemht[0].DiemQuaTrinh, lstDiemht[0].DiemThi, lstDiemht[0].DiemTongKet, lstDiemht[0].DiemThiLan2);
                }
                else
                {
                    DataSource.Rows.Add(item.PhieuDangKy.SinhVien.MSSV, item.PhieuDangKy.SinhVien.HoTen, string.Empty, string.Empty, string.Empty, string.Empty);
                }
            }
        }

        private void FrmBangDiemHocPhan_Load(object sender, EventArgs e)
        {
            InitHiddenFields();
            InitGridView();
            InitDataSource();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnInBangDiem_Click(object sender, EventArgs e)
        {
            if (DataSource == null)
            {
                MessageBoxEx.Show("Bạn chưa chọn học phần", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            FrmInBangDiemHocPhan frm = new FrmInBangDiemHocPhan
            {
                Khoa = txtKhoa.Text,
                ChuyenNganh = ((ChuyenNganh)cbxChuyenNganh.SelectedItem).TenCN,
                KHDT = ((KeHoachDaoTao)cbxKHDT.SelectedItem).TenGoi,
                MaHocPhan = ((ChiTietKHDT)cbxHocPhan.SelectedItem).MaHP.ToString(),
                TenHocPhan = ((ChiTietKHDT)cbxHocPhan.SelectedItem).HocPhan.TenHP,
                DataSource = this.DataSource
            };
            frm.ShowDialog();
        }
    }
}